
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import javax.swing.JFileChooser;

/*
 * Editor.java
 *
 * Created on 25 de julio de 2010, 09:06 PM
 */
import javax.swing.JOptionPane;
import javax.xml.transform.Source;



/**
 *
 * @author  HP_Administrator
 */
public class Editor extends javax.swing.JFrame {

    /** Creates new form Editor */
    public Editor() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        AreaT1 = new javax.swing.JTextArea();
        jScrollPane2 = new javax.swing.JScrollPane();
        consola = new javax.swing.JTextArea();
        jScrollPane3 = new javax.swing.JScrollPane();
        treeSpace = new javax.swing.JTree();
        jMenuBar1 = new javax.swing.JMenuBar();
        ArchivoMenu = new javax.swing.JMenu();
        Nuevo = new javax.swing.JMenuItem();
        Open = new javax.swing.JMenuItem();
        Guardar = new javax.swing.JMenuItem();
        ParseText = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        AreaT1.setColumns(20);
        AreaT1.setRows(5);
        jScrollPane1.setViewportView(AreaT1);

        consola.setColumns(20);
        consola.setEditable(false);
        consola.setRows(5);
        jScrollPane2.setViewportView(consola);

        //jScrollPane3.setViewportView(treeSpace);
        jScrollPane3.setViewportView(Descafeinado.jtree);

        ArchivoMenu.setText("File");

        Nuevo.setText("Nuevo");
        Nuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                NuevoActionPerformed(evt);
            }
        });
        ArchivoMenu.add(Nuevo);

        Open.setText("Abrir");
        Open.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                OpenActionPerformed(evt);
            }
        });
        ArchivoMenu.add(Open);

        Guardar.setText("Guardar");
        Guardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                GuardarActionPerformed(evt);
            }
        });
        ArchivoMenu.add(Guardar);

        ParseText.setText("Parse");
        ParseText.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ParseTextActionPerformed(evt);
            }
        });
        ArchivoMenu.add(ParseText);

        jMenuBar1.add(ArchivoMenu);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 738, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 436, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 292, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 408, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 408, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 178, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void OpenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_OpenActionPerformed
           
          JFileChooser selectorArchivo2 = new JFileChooser();
          selectorArchivo2.setFileSelectionMode(JFileChooser.FILES_ONLY);

          int resultado2 = selectorArchivo2.showOpenDialog( null );
       
          if (resultado2 == JFileChooser.CANCEL_OPTION)
             return;

          File nombreArchivo2 = selectorArchivo2.getSelectedFile(); 

          String strTexto; 
          StringBuffer sbBuffer = new StringBuffer();
          try{
               BufferedReader brEntrada = new BufferedReader(new FileReader(nombreArchivo2));
               while ((strTexto = brEntrada.readLine()) != null) 
                    sbBuffer.append( strTexto + "\n" );
              }  
          catch (IOException e){
               JOptionPane.showMessageDialog( null, "Archivo no encontrado ", "Archivo no encontrado ", JOptionPane.ERROR_MESSAGE );}
          
          String str = sbBuffer.toString();
          AreaT1.setText(str);
            

}//GEN-LAST:event_OpenActionPerformed

private void GuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_GuardarActionPerformed

	JFileChooser selectorArchivo = new JFileChooser();
    selectorArchivo.setFileSelectionMode( JFileChooser.FILES_ONLY );

    int resultado = selectorArchivo.showSaveDialog( null );

    if ( resultado == JFileChooser.CANCEL_OPTION )
         return;

    File nombreArchivo = selectorArchivo.getSelectedFile(); 

    String strTexto = AreaT1.getText();
    char cAux[] = strTexto.toCharArray();
    try {
         BufferedWriter bfSalida = new BufferedWriter(new FileWriter(nombreArchivo));
         for (int v=0; v < cAux.length; v++)
              bfSalida.write(cAux[v]);
         
              bfSalida.close();
         }
    catch (IOException e){}
    
    
}//GEN-LAST:event_GuardarActionPerformed

private void NuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_NuevoActionPerformed
    AreaT1.setText("");
}//GEN-LAST:event_NuevoActionPerformed

private void ParseTextActionPerformed(java.awt.event.ActionEvent evt) {                                          
    Descafeinado bean = new Descafeinado();
    bean.Parse();
    jScrollPane3.setViewportView(Descafeinado.jtree);
}

public static String getFileText(){
	return AreaT1.getText();
}

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
               new Editor().setVisible(true);
            }
        });
        
        
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenu ArchivoMenu;
    private static javax.swing.JTextArea AreaT1;
    private javax.swing.JMenuItem Guardar;
    private javax.swing.JMenuItem Nuevo;
    private javax.swing.JMenuItem Open;
    private javax.swing.JMenuItem ParseText;
    private javax.swing.JTextArea consola;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTree treeSpace;
    // End of variables declaration//GEN-END:variables

}
